package com.jolink.omiis.wechat.inner;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.socket.client.Ack;
import io.socket.client.IO;
import io.socket.client.IO.Options;
import io.socket.client.Socket;
import io.socket.emitter.Emitter.Listener;

/**
 * Hello world!
 *
 */
public class App {
	private static final Logger logger = LoggerFactory.getLogger(App.class);

	public static void main(String[] args) {
//		new URI(scheme, userInfo, host, port, path, query, fragment)
		try {
			
			URI uri = new URI("http", "", "121.40.224.134", 7005, "", "userId=omiis-wechat", "/");
			System.out.println(uri.toString());
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// try {
		// testSocketIO();
		// } catch (URISyntaxException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

	}

	public static final String EVENT_CUSTOM_MESSAGE = "bus-message";

	private static void testSocketIO() throws URISyntaxException {
		String address = "121.40.224.134";
		// final Socket socket = IO.socket("http://" + address +
		// ":7005?userId=omiis-wechat");
		IO.Options options = new Options();
		options.forceNew = true;
		options.port = 7005;
		options.host = address;
		options.reconnection = true;
		final Socket socket = IO.socket("http://" + address + ":7005?userId=omiis-wechat", options);
		socket.on(socket.EVENT_CONNECT, new Listener() {

			@Override
			public void call(Object... args) {
				logger.info("Event：{}，Msg：{}", "EVENT_CONNECT", Arrays.toString(args));
				logger.info("连接至全媒体Server");
				// TODO 此处编写：客户端建立与BUS的连接后，需要执行的代码
				BusMessage busMessage = new BusMessage();
				busMessage.setChannelId("WECHAT");
				busMessage.setMsgType("REGISTER");
				busMessage.setSenderId("WECHAT");
				busMessage.setSenderRole("");
				busMessage.setSenderIp("");
				socket.emit(EVENT_CUSTOM_MESSAGE, new Object[] { new JSONObject(busMessage) }, new Ack() {

					@Override
					public void call(Object... args) {
						logger.info("Emit返回信息：{}", Arrays.toString(args));
					}
				});
				logger.info("申请注册全媒体Server");
			}
		}).on(socket.EVENT_DISCONNECT, new Listener() {

			@Override
			public void call(Object... args) {
				logger.info("Event：{}，Msg：{}", "EVENT_DISCONNECT", Arrays.toString(args));

			}
		}).on(EVENT_CUSTOM_MESSAGE, new Listener() {

			@Override
			public void call(Object... args) {
				logger.info("Event：{}，Msg：{}", "EVENT_CUSTOM_MESSAGE", Arrays.toString(args));

			}
		}).on(socket.EVENT_RECONNECT, new Listener() {

			@Override
			public void call(Object... args) {
				logger.info("Event：{}，Msg：{}", "EVENT_RECONNECT", Arrays.toString(args));

			}
		}).on(socket.EVENT_CONNECT_ERROR, new Listener() {

			@Override
			public void call(Object... args) {
				logger.info("Event：{}，Msg：{}", "EVENT_CONNECT_ERROR", Arrays.toString(args));

			}
		}).on(socket.EVENT_CONNECT_TIMEOUT, new Listener() {

			@Override
			public void call(Object... args) {
				logger.info("Event：{}，Msg：{}", "EVENT_CONNECT_TIMEOUT", Arrays.toString(args));

			}
		}).on(socket.EVENT_RECONNECT_FAILED, new Listener() {

			@Override
			public void call(Object... args) {
				logger.info("Event：{}，Msg：{}", "EVENT_RECONNECT_FAILED", Arrays.toString(args));

			}
		}).on(socket.EVENT_RECONNECT_ERROR, new Listener() {

			@Override
			public void call(Object... args) {
				logger.info("Event：{}，Msg：{}", "EVENT_RECONNECT_ERROR", Arrays.toString(args));

			}
		}).on(socket.EVENT_RECONNECT_ERROR, new Listener() {

			@Override
			public void call(Object... args) {
				logger.info("Event：{}，Msg：{}", "EVENT_RECONNECT_ERROR", Arrays.toString(args));

			}
		}).on(socket.EVENT_MESSAGE, new Listener() {

			@Override
			public void call(Object... args) {
				logger.info("Event：{}，Msg：{}", "EVENT_MESSAGE", Arrays.toString(args));

			}
		}).on(socket.EVENT_RECONNECTING, new Listener() {

			@Override
			public void call(Object... args) {
				logger.info("Event：{}，Msg：{}", "EVENT_RECONNECTING", Arrays.toString(args));

			}
		});
		socket.connect();
	}
}
